package icu.qx.gbs.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import icu.qx.common.constant.TableNameEnum;
import icu.qx.common.utils.CodeUtils;
import icu.qx.gbs.domain.GbsGood;
import icu.qx.gbs.service.IGbsGoodService;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/gbs/good")
public class GbsGoodController extends BaseController {

    @Autowired
    private IGbsGoodService gbsGoodService;

    @Autowired
    private CodeUtils codeUtils;

    /**
     * 查询物品 通过 is_material 区分物料和产品列表
     */
    @GetMapping("/list")
    public TableDataInfo list(GbsGood gbsGood) {
        startPage();
        List<GbsGood> list = gbsGoodService.selectGbsGoodList(gbsGood);
        return getDataTable(list);
    }


    /**
     * 查找没有工艺路线的产品
     */
    @GetMapping("/nr")
    public AjaxResult goodNoRoute() {
        List<GbsGood> gbsGoods = gbsGoodService.selectGbsGoodNoRoute();
        return AjaxResult.success(gbsGoods);
    }

    /**
     * 导出物品 通过 is_material 区分物料和产品列表
     */
    @RequiresPermissions("gbs:good:export")
    @Log(title = "物品 通过 is_material 区分物料和产品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GbsGood gbsGood) {
        List<GbsGood> list = gbsGoodService.selectGbsGoodList(gbsGood);
        ExcelUtil<GbsGood> util = new ExcelUtil<GbsGood>(GbsGood.class);
        util.exportExcel(response, list, "物品 通过 is_material 区分物料和产品数据");
    }

    /**
     * 获取物品 通过 is_material 区分物料和产品详细信息
     */
    @GetMapping(value = "/{pdCode}")
    public AjaxResult getInfo(@PathVariable("pdCode") String pdCode) {
        return success(gbsGoodService.selectGbsGoodByPdCode(pdCode));
    }

    /**
     * 获取物品 通过 is_material 区分物料和产品详细信息
     */
    @GetMapping(value = "/route/{pdCode}")
    public AjaxResult getInfoByRoute(@PathVariable("pdCode") String routeCode) {
        return success(gbsGoodService.selectGbsGoodByRouteCode(routeCode));
    }

    /**
     * 新增物品 通过 is_material 区分物料和产品
     */
    @RequiresPermissions("gbs:good:add")
    @Log(title = "物品 通过 is_material 区分物料和产品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GbsGood gbsGood) {
        String pdCode = codeUtils.generateCode(TableNameEnum.GBS_GOOD.getTableName());
        gbsGood.setPdCode(pdCode);
        return AjaxResult.success(gbsGoodService.insertGbsGood(gbsGood));
    }

    /**
     * 修改物品 通过 is_material 区分物料和产品
     */
    @RequiresPermissions("gbs:good:edit")
    @Log(title = "物品 通过 is_material 区分物料和产品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GbsGood gbsGood) {
        return AjaxResult.success(gbsGoodService.updateGbsGood(gbsGood));
    }

    /**
     * 修改物品 通过 is_material 区分物料和产品
     */
    @RequiresPermissions("gbs:good:edit")
    @Log(title = "物品 通过 is_material 区分物料和产品", businessType = BusinessType.UPDATE)
    @PutMapping("/route")
    public AjaxResult editRoute(@RequestBody List<GbsGood> gbsGoods) {
        return AjaxResult.success(gbsGoodService.updateGbsGoodRoute(gbsGoods));
    }

    /**
     * 删除物品 通过 is_material 区分物料和产品
     */
    @RequiresPermissions("gbs:good:remove")
    @Log(title = "物品 通过 is_material 区分物料和产品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{pdCodes}")
    public AjaxResult remove(@PathVariable String[] pdCodes) {
        return toAjax(gbsGoodService.deleteGbsGoodByPdCodes(pdCodes));
    }

}
